<template>
  <div class="index">
    <div class="homeContainer">
      <img src="@/assets/images/qsxc.png" />
      <div class="qrCodeContainer">
        <canvas id="qrcode" ref="qrcodeCanvas"></canvas>
        <div class="tip_bg" v-if="showTip">
          <div class="btn" @click="goLogin">登录</div>
          <div class="tips">生成正式推广码</div>
        </div>
      </div>
    </div>
    <div class="downLoad" @click="downLoad">下载二维码</div>
  </div>
</template>
<script lang="ts">
//@ts-nocheck
import QRCode from "qrcode";
import { useRouter } from "vue-router";
import html2canvas from 'html2canvas';

export default {
  name: "App",
  setup() {
    const router = useRouter()
    const goLogin = () => {
      router.push({ path: '/login' })
    }
    return {
      goLogin
    };
  },
  data() {
    return {
      showTip: true
    }
  },
  mounted() {
    let id = window.userInfo && window.userInfo.userInfo.id;
    let txt = 'http://app.fangtuguyu.com/share/index.html?qsxcPromoterId=' + id;
    const canvas = this.$refs.qrcodeCanvas;
    let options = {
      width: canvas.offsetHeight,
      height: canvas.offsetHeight,
      margin: 0
    }

    QRCode.toCanvas(canvas, txt, options, error => {
      if (error) console.error(error)
    })
    if (id) {
      this.showTip = false
    }

  },
  methods: {
    downLoad() {
      html2canvas(document.getElementsByClassName("homeContainer")[0]).then(canvas => {
        document.body.appendChild(canvas); // 将生成的canvas添加到body中
        var dataURL = canvas.toDataURL('image/png');
        const link = document.createElement('a');
        link.download = 'qrcode';
        link.href = dataURL;
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.index {
  width: 100vw;
  overflow: hidden;
  position: relative;

  img {
    position: relative;
    width: 100vw;
  }

  .qrCodeContainer {
    position: absolute;
    left: 16vw;
    top: 46vw;
    width: 29vw;
    height: 29vw;
    background-color: red;
  }

  .homeContainer {
    height: 100vw;
    width: 100vw;
  }
  #qrcode {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
  }

  .tip_bg {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba($color: #000000, $alpha: 0.7);
    text-align: center;

    .btn {
      position: relative;
      top: 4vw;
      color: white;
      font-size: 24px;
      width: 15vw;
      left: 7vw;
      border-bottom: 1px solid white;
    }

    .tips {
      position: relative;
      top: 8vw;
      color: white;
    }


  }
  .downLoad {
    margin:auto;
      margin-top: 30px;
      width: 40vw;
      height: 38px;
      background: rgba(255, 255, 255, 0.3);
      border-radius: 6px;
      border: 1px solid rgba(255, 255, 255, 0.2);
      text-align: center;
      line-height: 38px;
      font-size: 18px;
      background: #169eff;
      color: white;
      font-weight: bold;
    }
}
</style>